package com.lhn.netty.netty_demo.inboundhandlerandoutboudhhandler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author lhn
 * @date 2025/06/11 19:29
 * @since 1.0.0
 **/
public class MyByteToLongDecoder extends ByteToMessageDecoder {

    /**
     * @param channelHandlerContext 上下文
     * @param byteBuf 缓存
     * @param list 存放解码后的数据
     * @throws Exception 异常
     */
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List <Object> list) throws Exception {
        System.out.println("MyByteToLongDecoder decode 被调用");
        // 添加解码逻辑 因为8个字节才能转换成一个long
        while (byteBuf.readableBytes() >= 8) {
            list.add(byteBuf.readLong());
        }
    }
}